package cj.web.admin.controllers;

import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.paoding.rose.web.annotation.Param;
import net.paoding.rose.web.annotation.Path;
import net.paoding.rose.web.annotation.rest.Get;
import net.paoding.rose.web.var.Model;

import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;

import cj.utils.DateUtils;
import cj.utils.ExportExcel;
import cj.web.admin.dao.ComposeOnlineOrderDAO;
import cj.web.admin.dao.GameDAO;
import cj.web.admin.domain.ComposeOnlineOrder;
import cj.web.admin.domain.Game;
import cj.web.constants.MemuEnum;
import cj.web.constants.PayStatusEnum;
import cj.web.login.controllers.BaseController;

@Path("onlineOrderExport")
public class COnlineOrderExportController extends BaseController {

	private static Logger logger = Logger
			.getLogger(COnlineOrderExportController.class.getName());

	private final static int excelCount = 50000;

	@Autowired
	private ComposeOnlineOrderDAO composeOnlineOrderDao;
	@Autowired
	private GameDAO gameDao;

	@Get("show")
	public String show(Model model) {
		model.add("MemuEnum", MemuEnum.wlzwcsxsdc);
		List<Game> objs = gameDao.queryAll();
		model.add("gameObjs", objs);
		return "/views/online/onlineOrderExportList.vm";
	}

	@Get("output")
	public void output(@Param("gameId") int gameId, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Game game = gameDao.queryById(gameId);

		String title = URLEncoder.encode(game.getName(), "UTF-8"); // 进行转码，使其支持中文文件名
		response.setContentType("application/vnd.ms-excel"); // 生成提示信息
		response.setHeader("content-disposition", "attachment;filename="
				+ title + DateUtils.getNowTime(DateUtils.DATE_FULL_STR1)
				+ ".xls");
		OutputStream fOut = response.getOutputStream();

		// 参赛证总数
		int count = composeOnlineOrderDao.queryCOnlineOrderSize(gameId);
		double sheetSize = Double.parseDouble(String.valueOf(count)) / excelCount;
		int sheetCount = (int) Math.ceil(sheetSize);

		ExportExcel<ComposeOnlineOrder> ex = new ExportExcel<ComposeOnlineOrder>();
		// 声明一个工作薄
		HSSFWorkbook workbook = new HSSFWorkbook();

		for (int i = 0; i < sheetCount; i++) {

			List<Map> dataset = null;

			// 信息Export
			//orderNo,name,identityCard,parentName,parentPhone,teacher,area,school,grade
			String[] headers = { "订单编号","参赛证号","参赛人姓名","参赛人身份证号","参赛人父母",
					"参赛人父母电话","参赛人老师","参赛人所在区域","参赛人所在学校","参赛人所在年级","付款状态","成绩" };
			String[] fields = { "orderNo","code","name","identityCard","parentName",
					"parentPhone","teacher","area","school","grade","statusName","score" };

			dataset = composeOnlineOrderDao.queryCOnlineOrderByGameId(gameId, i * excelCount, excelCount);
			for (Map rep : dataset) {
				String statusName = PayStatusEnum.getName(Integer.valueOf(rep.get("status").toString()));
				rep.put("statusName", statusName);
			}

			ex.exportExcelMore(workbook,game.getName() + "-" + i * excelCount, headers, fields,
					dataset, DateUtils.DATE_FULL_STR);
		}
		
		try {
			//关闭工作薄
			workbook.write(fOut);
		} catch (IOException e) {
			e.printStackTrace();
		}

		fOut.flush();
		fOut.close();
	}
}